package sqlinjection;

import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.ws.rs.core.Response;

/**
 *
 * @author Atuan
 */
@ManagedBean(name="movieService")
@SessionScoped
public class ManageMoviesService {
    
    
    private Movie currentMovie=new Movie();
    private Movie editedMovie=new Movie();
    private String searchQuery="";
    private List<Movie> allmovies = new ArrayList<>();
    private List<Movie> movies = new ArrayList<>();
    private List<Movie> movies2 = new ArrayList<>();
    private String message1 = "";
    private String message2 = "";
    
    public String editMovieNotSafely(UserService us){
        System.out.println("login: "+us.getCurrentUser().getLogin());
        editedMovie.setContributor(us.getCurrentUser().getLogin());
        DBHandler.editMovieNotSafely(editedMovie, us.getCurrentUser());
        return "";
    }
    
    public void updateEditedMovie(int id){
        editedMovie = DBHandler.getMovieById(id);
    }
    
    public String editMovie(UserService us){
        editedMovie.setContributor(us.getCurrentUser().getLogin());
        DBHandler.editMovieNotSafely(editedMovie, us.getCurrentUser());
        return "";
        
    }
    
    public void addMovie(UserService us){
        currentMovie.setContributor(us.getCurrentUser().getLogin());
        if(DBHandler.addMovie(currentMovie)){
            FacesMessage message = new FacesMessage();
            message.setSeverity(FacesMessage.SEVERITY_INFO);
            message.setSummary("Movie was added to the database");
            message.setDetail(currentMovie.toString());
            FacesContext.getCurrentInstance().addMessage("messages", message);
            resetFields();
        }
    }
    
    public void addMovieVulnerable(UserService us){
        currentMovie.setContributor(us.getCurrentUser().getLogin());
        if(DBHandler.addMovieVulnerable(currentMovie)){
            FacesMessage message = new FacesMessage();
            message.setSeverity(FacesMessage.SEVERITY_INFO);
            message.setSummary("Movie was added to the database");
            message.setDetail(currentMovie.toString());
            FacesContext.getCurrentInstance().addMessage("messages", message);
            resetFields();
        }
    }
    
    public List<Movie> listAllMovies(){
        return DBHandler.getAllMovies();
    }
    
    /**
     * method responsible for retrieving movie with a given title, susceptible for sql injection
     * @return 
     */
    public void getMovieByTitle(){
        movies = DBHandler.getMovieByTitle(searchQuery);
        if(movies.isEmpty()){
            message1 = "No movie matching this title was found.";
        } else {
            message1 = "Weak search, number of results: "+movies.size();             
        }
        System.out.println("MOVIES: "+movies.size());
    }
    
    /**
     * method responsible for retrieving movie with a given title, NOT susceptible for sql injection
     * @return 
     */
    public void getMovieByTitle2(){
        movies = DBHandler.getMovieByTitle2(searchQuery);
        if(movies.isEmpty()){
            message1 = "No movie matching this title was found.";
        } else {
            message1 = "Better search, number of results: "+movies.size(); 
        }
        System.out.println("MOVIES2: "+movies.size());
    }
    
    private void resetFields(){
        currentMovie = new Movie();
    }

    public Movie getCurrentMovie() {
        return currentMovie;
    }

    public void setCurrentMovie(Movie currentMovie) {
        this.currentMovie = currentMovie;
    }
    
    public String getSearchQuery() {
        return searchQuery;
    }

    public void setSearchQuery(String searchQuery) {
        this.searchQuery = searchQuery;
    }

    public List<Movie> getMovies() {
        return movies;
    }

    public void setMovies(List<Movie> movies) {
        this.movies = movies;
    }

    public List<Movie> getMovies2() {
        return movies2;
    }

    public void setMovies2(List<Movie> movies2) {
        this.movies2 = movies2;
    }

    public String getMessage1() {
        return message1;
    }

    public void setMessage1(String message1) {
        this.message1 = message1;
    }

    public String getMessage2() {
        return message2;
    }

    public void setMessage2(String message2) {
        this.message2 = message2;
    }

    public List<Movie> getAllmovies() {
        return allmovies;
    }

    public void setAllmovies(List<Movie> allmovies) {
        this.allmovies = allmovies;
    }

    public Movie getEditedMovie() {
        return editedMovie;
    }

    public void setEditedMovie(Movie editedMovie) {
        this.editedMovie = editedMovie;
    }
    
}
